﻿using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace JiveMessenger.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class AddEmoticonPage : BasicPage
    {
        public AddEmoticonPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            Tuple<bool, StorageFile> param = e.Parameter as Tuple<bool, StorageFile>;

            this.TextBoxPageHeader.Opacity = 0;
            base.OnNavigatedTo(e);
            if (param == null || param.Item1 == false)
            {
                await StatusBar.GetForCurrentView().HideAsync();
                await Task.Delay(800);
                Storyboard sb = this.Resources["ShowPageHeaderAnim"] as Storyboard;
                sb.Begin();
            }
            else
            {
                this.TextBoxPageHeader.Opacity = 1;
            }
            if (param != null)
            {
                await ((ViewModelAddEmoticon)this.DataContext).ProcessEmoticonImage(param.Item2);
                if (this.Frame.CanGoBack)
                    this.Frame.BackStack.RemoveAt(this.Frame.BackStackDepth-1);
                if (this.Frame.CanGoBack)
                    this.Frame.BackStack.RemoveAt(this.Frame.BackStackDepth - 1);
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {

            base.OnNavigatedFrom(e);

            ((ViewModelAddEmoticon)this.DataContext).Cleanup();
        }
    }
}
